/*
 * InputSingleAnswerJPanel.java
 *
 * Created on 16.01.2009, 20:29:17
 */
package morenoapp.components.input;

import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import morenoapp.model.data.MorenoQuestionFace;
import morenoapp.model.data.MorenoQuestionItemFace;
import morenoapp.model.data.MorenoRespondentFace;
import morenoapp.util.I18N;

/**
 * 
 * @author dmitry.mamonov
 */
public class InputSingleAnswerJPanel extends javax.swing.JPanel implements MorenoRespondentSelecetionListener {

    /**
     * Вопрос которому соответствует данная форма.
     */
    MorenoQuestionFace question = null;
    /**
     * Список полей с ответами.
     */
    List<InputSingleAnswerItemTextFieldJPanel> answersItemsList = new ArrayList<InputSingleAnswerItemTextFieldJPanel>();

    /** Creates new form InputSingleAnswerJPanel */
    public InputSingleAnswerJPanel() {
        initComponents();
        postInitComponents();
    }

    InputSingleAnswerJPanel(MorenoQuestionFace question) {
        initComponents();
        this.question = question;
        postInitComponents();
    }

    private void postInitComponents() {
        jPanelItems.setLayout(new BoxLayout(jPanelItems, BoxLayout.Y_AXIS));
        initPanel();
    }

    public MorenoQuestionFace getQuestion() {
        return question;
    }

    public void setQuestion(MorenoQuestionFace question) {
        this.question = question;
        initPanel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelCaption = new javax.swing.JLabel();
        jPanelItems = new javax.swing.JPanel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(morenoapp.MorenoApp.class).getContext().getResourceMap(InputSingleAnswerJPanel.class);
        jLabelCaption.setText(resourceMap.getString("jLabelCaption.text")); // NOI18N
        jLabelCaption.setName("jLabelCaption"); // NOI18N

        jPanelItems.setName("jPanelItems"); // NOI18N

        javax.swing.GroupLayout jPanelItemsLayout = new javax.swing.GroupLayout(jPanelItems);
        jPanelItems.setLayout(jPanelItemsLayout);
        jPanelItemsLayout.setHorizontalGroup(
            jPanelItemsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jPanelItemsLayout.setVerticalGroup(
            jPanelItemsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelItems, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabelCaption, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelCaption)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelItems, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void initPanel() {
        //jPanelItems.setBackground(Color.BLACK);
        jPanelItems.removeAll(); //TODO например здесь нужно осовобождать ссылки
        answersItemsList.clear();

        if (question != null) {
            jLabelCaption.setText("<html>"+question.toHtmlString()); //NOI18N
            for (MorenoQuestionItemFace questionItem : question.getQuestionItemsList()) {
                InputSingleAnswerItemTextFieldJPanel answerItem = new InputSingleAnswerItemTextFieldJPanel();
                answerItem.setQuestion(question);
                answerItem.setQuestionItem(questionItem);
                answersItemsList.add(answerItem);
                jPanelItems.add(answerItem);
            }
            jPanelItems.invalidate();
            jPanelItems.validate();
            //jPanelItems.add(new JPanel());

            //jPanelItems.setMaximumSize(new Dimension(jPanelItems.getWidth(),(int) jPanelItems.getPreferredSize().getHeight()));


            if (getParent() != null) {
                //setMaximumSize(new Dimension((int) getMaximumSize().getWidth(), jPanelItems.getY() + jPanelItems.getHeight()));
                getParent().invalidate();
                getParent().validate();
            }
        } else {
            jLabelCaption.setText(I18N.get("label.question_not_specified_brackets"));
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelCaption;
    private javax.swing.JPanel jPanelItems;
    // End of variables declaration//GEN-END:variables

    public void onRespondentSelected(MorenoRespondentFace aRespondent) {
        for (InputSingleAnswerItemTextFieldJPanel answerItem : answersItemsList) {
            answerItem.onRespondentSelected(aRespondent);
        }
    }
}
